﻿Zepto(function ($) {
    var aid = $("#aid").val();
    FastClick.attach(document.body);

    var _w = $('.wrapper').width(), picH = (_w - 10 - 5 - 10) / 2;
    if (_w > 500) {
        picH = (_w - 10 - 5 - 5 - 10) / 3;
    }
    $('.product-item-t2').css('width', picH + 'px');
    $('.product-item-t2 .pic').css({ 'width': picH + 'px', 'height': picH + 'px' });
    Util.lazyLoad('lazy');

    var couponList = $('#couponList'), loadingText = $('.loading-text');
    var page = 0, pagesize = 20, load = 1, cate = 0,
        type = 'last', channel = 35, gender = 0;

    $('.product-list-area').css('min-height', (document.documentElement.clientHeight - 40) + 'px');

    var k9TabItem = $('.k9-tab-area .tab-item');
    k9TabItem.on('click', function () {
        var _this = $(this);
        if (!_this.hasClass('active')) {
            $('.k9-tab-area .tab-item.active').removeClass('active');
            _this.addClass('active');

            type = _this.data('type');
            cate = 0;
            $('.cate-item.active').removeClass('active');
            $($('.cate-item')[0]).addClass('active');
            page = 0;
            load = 1;
            loadingText.html('努力加载中...');
            couponList.html('');
            $('body').scrollTop(0);
            setTimeout(getCouponList, 30);
        }
    });

    var cateArea = $('.cate-area'),
        cateAreaInner = $('.cate-area .inner'),
        cateItem = $('.cate-item');
    cateItem.on('click', function () {
        var _this = $(this);
        if (!_this.hasClass('active')) {
            $('.cate-item.active').removeClass('active');
            _this.addClass('active');

            var l = _this.offset().left - cateAreaInner.offset().left;
            cateAreaInner.scrollLeft(cateAreaInner.scrollLeft() + l - _w / 2 + 20);

            cate = _this.data('cate-id');
            page = 0;
            load = 1;
            loadingText.html('努力加载中...');
            couponList.html('');
            $('body').scrollTop(0);
            setTimeout(getCouponList, 30);
        }
    });

    getCouponList();

    Util.scrollEvent(getCouponList, 300);

    function getCouponList() {
        if (!load) {
            return;
        }
        $.ajax({
            url: '/' + aid + '/getZhekouListByCate.html',
            type: 'Post',
            dataType: 'json',
            data: {
                'type': type,
                'limit': page,
                'pagesize': pagesize,
                'cid': cate,
                'gender': gender,

            },
            beforeSend: function () {
                load = 0;
            },
            success: function (data) {
                if (data.rows.CurrentPageList != null) {
                    createCouponListHtml(data.rows.CurrentPageList);
                    if (data.rows.CurrentPageList.length >= pagesize) {

                        page++;
                        load = 1;
                    } else {
                        loadingText.html('没有更多了');
                    }
                } else {
                    loadingText.html('没有更多了');
                }
            }
        });
    }

    function createCouponListHtml(clist) {
        var htmlstr = '';
        for (var i = 0, len = clist.length; i < len; i++) {
            var zk = clist[i];
            htmlstr += '<a href="/' + aid + '/goods/' + zk.auctionId + '.html">';
            htmlstr += '<div class="product-item-t2" style="width:' + picH + 'px;">';
            htmlstr += '<div class="pic lazy new" data-original="' + zk.pictUrl + '" style="width: ' + picH + 'px; height: ' + picH + 'px;"></div>';
            htmlstr += '<p class="title">';
            //if (type == 'brand' && zk.brand.brand_id) {
            //htmlstr += '<span class="brand">' + zk.brand.name + '</span>';
            // } else if (zk.post_free == 1) {
            htmlstr += '<span class="post-free">包邮</span>&nbsp;';
            //}
            htmlstr += zk.title + '</p>';
            if (type == 'speed') {
                htmlstr += '<div class="progress-area">';
                htmlstr += '<p class="pro-text">已领' + Math.floor(zk.couponTotalCount * 100) + '%</p>';
                htmlstr += '<div class="pro-container">';
                htmlstr += '<div data-pro="' + zk.progress + '" class="pro" style="width:' + Math.floor(zk.couponTotalCount * 100) + '%;"></div>';
                htmlstr += '</div>';
                htmlstr += '</div>';
            } else {
                htmlstr += '<p class="sold">' + (zk.platform_id == 2 ? '淘宝' : '天猫') + '价&nbsp;&yen;' + zk.zkPrice + '</p>';
            }
            htmlstr += '<div class="price-area">';
            htmlstr += '<p class="gap-price"><em class="number-font">' + zk.couponAmount + '</em>元券</p>';
            htmlstr += '<p class="price">券后价&yen;<em class="number-font">' + (zk.zkPrice - zk.couponAmount).toFixed(2) + '</em></p>';
            htmlstr += '</div>';
            htmlstr += '</div>';
            htmlstr += '</a>';
        }
        couponList.append(htmlstr);
        Util.lazyLoad('lazy.new');
        $('.lazy.new').removeClass('new');
    }

    var countdown = function (obj, endDesc) {
        if (!obj) {
            return;
        }
        obj = $(obj);
        var endTime = obj.data('endtime');
        var now = Math.floor(new Date().getTime() / 1000) * 1;
        if (now > endTime) {
            obj.html(endDesc);
        } else {
            var gap = endTime - now;
            var dd = Math.floor(gap / (60 * 60 * 24));
            var hh = Math.floor((gap - dd * 60 * 60 * 24) / (60 * 60));
            var mm = Math.floor((gap - dd * 60 * 60 * 24 - hh * 60 * 60) / 60);
            var ss = gap - dd * 60 * 60 * 24 - hh * 60 * 60 - mm * 60;
            var timeStr = '距结束&nbsp;'
                + (hh > 0 ? '<em>' + (hh >= 10 ? hh : '0' + hh) + '</em>:' : '')
                + (mm > 0 ? '<em>' + (mm >= 10 ? mm : '0' + mm) + '</em>:' : '')
                + (ss >= 0 ? '<em>' + (ss >= 10 ? ss : '0' + ss) + '</em>' : '');
            obj.html(timeStr);
        }
    }
    if ($('.count-down').length > 0) {
        countdown($('.count-down'), '已结束');
        setInterval(function () {
            countdown($('.count-down'), '已结束');
        }, 1000);
    }
});
function callbackHandler() { }